package com.rfid.analysis;

import com.rfid.protocol.Instruction;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2020/9/16
 * 需要被处理的消息体
 */
public class Message {
    private ByteBuffer mBuffer = ByteBuffer.allocate(2048);
    private Handler mCurrentNodeHandler;
    private Handler mFirstHandler;
    private int position;
    private boolean hasComplete;
    private List<Byte> allData = new ArrayList<>();
    private byte[] data;
    private int dataLength;
    private byte address;
    private Instruction mCommand;

    public boolean isHasComplete() {
        return hasComplete;
    }

    public void put(byte[] bytes) {
        try {
            mBuffer.put(bytes);
            mBuffer.flip();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public Instruction getCommand() {
        return mCommand;
    }

    public void setCommand(Instruction command) {
        mCommand = command;
    }

    public void addData(byte[] bytes) {
        for (byte b : bytes) {
            allData.add(b);
        }
    }

    public void setDataLength(int dataLength) {
        this.dataLength = dataLength;
    }

    public byte getAddress() {
        return address;
    }

    public void setAddress(byte address) {
        this.address = address;
    }

    public int getDataLength() {
        return dataLength;
    }

    public List<Byte> getAllData() {
        return allData;
    }


    public Handler getFirstHandler() {
        return mFirstHandler;
    }

    public void setFirstHandler(Handler firstHandler) {
        mFirstHandler = firstHandler;
    }

    public void setHasComplete(boolean hasComplete) {
        this.hasComplete = hasComplete;
        if (hasComplete) {
            if (mCommand != null) {
                mCommand.invoke(getData());
            }
        } else {
            allData.clear();
        }
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public ByteBuffer getBuffer() {
        return mBuffer;
    }

    public Handler getCurrentNodeHandler() {
        return mCurrentNodeHandler;
    }

    public void setCurrentNodeHandler(Handler currentNodeHandler) {
        mCurrentNodeHandler = currentNodeHandler;
    }
}